package demo1;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 两个队列模拟实现一个栈
 */
class MyStack {
    Queue<Integer> qu1 ;
    Queue<Integer> qu2 ;


    public MyStack() {
        qu1 = new LinkedList<>();
        qu2= new LinkedList<>();

    }
    
    public void push(int x) {
        if(empty()){
            qu1.offer(x);
            }else {
            if (!qu1.isEmpty()) {
                qu1.offer(x);
            } else {
                qu2.offer(x);
            }
        }
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }
        int size = 0 ;
        if(!qu1.isEmpty()){
            size = qu1.size();
            for (int i = 0; i < size-1; i++) {
                qu2.offer(qu1.poll());


            }
            return qu1.poll();
        }else{
            size =qu2.size();
            for (int i = 0; i < size-1; i++) {
                qu1.offer(qu2.poll());


            }
            return qu2.poll();
        }


    }
    
    public int top() {
        if(empty()){
            return  -1;
        }
        int size = 0 ;
        int tmp =0;
        if(!qu1.isEmpty()){
            size = qu1.size();
            for (int i = 0; i < size; i++) {
                tmp = qu1.poll();
                qu2.offer(tmp);



            }
            return tmp;
        }else{
            size =qu2.size();
            for (int i = 0; i < size; i++) {
                tmp = qu2.poll();
                qu1.offer(tmp);


            }
            return tmp;
        }


    }
    
    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();

    }
}